package com.dongdongshop.controller;

import cn.hutool.core.lang.Validator;
import com.alibaba.fastjson.JSONObject;
import com.dongdongshop.admin.model.TbUser;
import com.dongdongshop.admin.vo.TbUserVO;
import com.dongdongshop.common.Result;
import com.dongdongshop.em.ResultEnum;
import com.dongdongshop.em.TokenEnum;
import com.dongdongshop.service.TUserservice;
import com.dongdongshop.shiro.LoginToken;
import com.dongdongshop.util.JwtUtil;
import com.dongdongshop.util.LoginUser;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("loginuser")
@Slf4j
public class LoginUserController {
    @Autowired
    private JwtUtil jwtUtil;
    @Autowired
    private TUserservice tUserservice;
    @Autowired
    private RocketMQTemplate rocketMQTemplate;
    @Autowired
    private StringRedisTemplate redisTemplate;
    @PostMapping("login")
    public Result login(@RequestParam("uname") String username, @RequestParam("pwd") String password){
        //获取subject
        Subject subject = SecurityUtils.getSubject();
        //封装usernamepasswordToken
        LoginToken usernamePasswordToken = new LoginToken(username,password,TokenEnum.USER);
        try {
            subject.login(usernamePasswordToken);
        }catch (UnknownAccountException e) {
            log.error("登录失败,原因{}",e);
            return Result.Response(ResultEnum.LOGIN_IS_NULL_ERROR);
        }catch (IncorrectCredentialsException e){
            log.error("登录失败,原因{}",e);
            return Result.Response(ResultEnum.LOGIN_IS_NULL_ERROR);
        }
        //登陆成功,封装jwt,将token返回给前段
        TbUser user = (TbUser) SecurityUtils.getSubject().getPrincipal();
        LoginUser loginUser = new LoginUser();
        loginUser.setUid(user.getId().toString());
        loginUser.setUname(user.getUsername());
        String token = jwtUtil.createJwt(loginUser.getUid().toString(), JSONObject.toJSONString(loginUser), "loginUser");
        return Result.OK().setData(token);
    }

    //注册
    @PostMapping("addUser")
    public Result addUser(@RequestBody TbUserVO tbUserVO){
        List<TbUserVO> list = tUserservice.getAll();
        for (TbUserVO tbSeller : list) {
            if (tbSeller.getUsername().equals(tbUserVO.getUsername())) {
                return Result.Response(ResultEnum.REGISTER_USER_IS_EXIST);
            }
        }
        if (redisTemplate.hasKey(tbUserVO.getPhone())){
            String s = redisTemplate.opsForValue().get(tbUserVO.getPhone());
            if (tbUserVO.getCaptcha().equals(s)){
                tUserservice.addTbUser(tbUserVO);
                return Result.OK();
            }
            return Result.Response(ResultEnum.CAPTCHA_ERROR);
        }else {
            return Result.Response(ResultEnum.NOTCAPTCHA_ERROR);
        }
    }

    @PostMapping("toMq")
    public Result toMq(@RequestParam String phoneNum){
        if(Validator.isMobile(phoneNum)){
            rocketMQTemplate.convertAndSend("dk2208-topic",phoneNum);
            return Result.OK();
        };
        return Result.Response(ResultEnum.PHONE_ERROR);
    }
}
